package com.bac.bacplatform;

import com.bac.utils.Util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public abstract class AbstructCommonActivity extends Activity{

	private MyHandler handler = new MyHandler();
	
	private Dialog mProgressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		// 进度条
		RelativeLayout rl = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.loading, null);
		mProgressDialog = new Dialog(this, R.style.dialog);
		mProgressDialog.getWindow().setContentView(rl);
		
	}
	
	@SuppressLint("NewApi")
	public void showAlertDialog(String title, String message){
		AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Custom_dialog);
		final AlertDialog alertDialog = builder.create();
//		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.show();
		Window window = alertDialog.getWindow();
		View view = LayoutInflater.from(this).inflate(R.layout.dialog_alert, null);
		((TextView)(view.findViewById(R.id.tv_title))).setText(title);
		((TextView)(view.findViewById(R.id.tv_message))).setText(message);
		view.findViewById(R.id.ly_ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDialog.dismiss();
			}
		});
		window.setContentView(view);
		window.setLayout((int)(Util.getWidth(getApplicationContext()) * 0.8), WindowManager.LayoutParams.WRAP_CONTENT);
	}
	
	public void showLoadingProgress(){
		sendMessage(MyHandler.SHOW_PROGRESS);
	}
	
	public void dismissLoadingProgressNow(){
		sendMessage(MyHandler.DISMISS_PROGRESS);
	}
	
	public void dismissLoadingProgress() {
		sendMessageDely(MyHandler.DISMISS_PROGRESS, 600);
	}

	protected void handleOtherMessage(int flag) {

	}

	public void sendMessage(int flag) {
		handler.sendEmptyMessage(flag);
	}
	
	public void sendMessageDely(int flag, long delayMillis) {
		handler.sendEmptyMessageDelayed(flag, delayMillis);
	}

	public void showToast(String toast_message) {
		handler.toast_message = toast_message;
		sendMessage(MyHandler.SHOW_STR_TOAST);
	}

	public void showToast(int res) {
		handler.toast_res = res;
		sendMessage(MyHandler.SHOW_RES_TOAST);
	}

	@SuppressLint("HandlerLeak")
	private class MyHandler extends Handler {
		public static final int SHOW_STR_TOAST   = 0;
		public static final int SHOW_RES_TOAST   = 1;
		public static final int SHOW_PROGRESS    = 2;
		public static final int DISMISS_PROGRESS = 3;

		private String toast_message = null;
		private int toast_res;

		@Override
		public void handleMessage(Message msg) {
			if (!Thread.currentThread().isInterrupted()) {
				switch (msg.what) {
				case SHOW_STR_TOAST:
					Toast.makeText(getBaseContext(), toast_message, Toast.LENGTH_SHORT).show();
					break;
				case SHOW_RES_TOAST:
					Toast.makeText(getBaseContext(), toast_res, Toast.LENGTH_SHORT).show();
					break;
				case SHOW_PROGRESS:
					if (mProgressDialog != null && !mProgressDialog.isShowing() ) {
						mProgressDialog.show();
					}
					break;
				case DISMISS_PROGRESS:
					if (mProgressDialog != null && mProgressDialog.isShowing()) {
						mProgressDialog.dismiss();
					}
					break;
				default:
					handleOtherMessage(msg.what);
				}
			}
		}
	}
	
}
